package com.jsle.financial.service;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.jsle.financial.dao.UsergroupDaoImpl;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.UserGroup;
import com.jsle.financial.manager.UsergroupManager;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.Result;
/**
 * UserService业务逻辑处理类
 * @author wisdying
 * @date 2012-10-27
 */
@Service
public class UsergroupServiceImpl implements UsergroupService{

	private Log log = LogFactory.getLog(UsergroupServiceImpl.class);
	
	@Resource
	private UsergroupManager usergroupManager;
	@Resource
	private UsergroupDaoImpl usergroupDao;
	
	/**
	 * 新增一用户组
	 * @param user
	 * @return
	 */
	public Result addUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			result = usergroupManager.addUserGroup(usergroup);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_INSERT_SERVICE_ERROR_MESSAGE, "User"));
			log.error("new usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户组
	 * @param user
	 * @return
	 */
	public Result delUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			result = usergroupManager.delUserGroup(usergroup);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_DEL_SERVICE_ERROR_MESSAGE, "User"));
			log.error("del usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户组信息
	 * @param user
	 * @return
	 */
	public Result updateUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			result = usergroupManager.updateUserGroup(usergroup);

		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_UPDATE_SERVICE_ERROR_MESSAGE, "User"));
			log.error("update usergroup error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 获取一用户组信息
	 * @param loginName
	 * @return
	 */
	public Result selUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			result = usergroupDao.selUserGroup(usergroup);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("query usergroup error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 分页查询
	 * @param page
	 * @return
	 */
	public Result queryUserGroupsByPage(Page page,String columnName,String paramName){
		Result result = new Result();
		try {
			result = usergroupDao.queryUserGroupsByPage(page,columnName,paramName);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "Query Users by page"));
			log.error("query usergroups by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 删除用于组与用户关联
	 * @param usergroup
	 * @return
	 */
	public Result delUserUserGroups(UserGroup ug){
		Result result = new Result();
		try {
			result = usergroupDao.delUserUserGroups(ug);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "delUserUserGroups"));
			log.error("delUserUserGroupse error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据用户Id获取用户及用户组信息
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryUserAndUserGroupByUser(User user){
		Result result = new Result();
		try {
			result = usergroupDao.queryUserAndUserGroupByUser(user);
		} catch (Exception e) {
			result.setResultCode(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Service.FC_RESULT_QUERY_SERVICE_ERROR_MESSAGE, "User"));
			log.error("queryUserAndUserGroupByUser error## "+ e.getMessage());
		}
		return result;
	}
}
